/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package educationTool.control;

import educationTool.model.Admin;
import educationTool.model.User;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 * Admin logic class
 * @author Pavel
 */
public class AdminController {

    private Admin admin = Admin.getInstance(); 
            
        
    public AdminController() {
        this.admin = Admin.getInstance();
    }
    

    public boolean createUser(String userLogin, String userPassword) {
        Core core = Core.getInstance();
        for (User user : core.getUsersList()) {
            if (userLogin.equals(user.getLogin())) {
                return false;
            }
        }
        core.getUsersList().add(new User(userLogin, getPasswordHash(userPassword)));
        return true;
    }
    
    public boolean deleteUser(String login) {
        Core core = Core.getInstance();
        User user = null;
        boolean isFound = false;

        for (User i : core.getUsersList()) {
            if (i.getLogin().equals(login)) {
                user = i;
                isFound = true;
            }
        }
        if (isFound) {
            core.getUsersList().remove(user);
            return true;
        } else {
            return false;
        }
    }

    public boolean changeUserPassword(User user, String newPassword) {
        Core core = Core.getInstance();
        if (core.getUsersList().contains(user)) {
            for (User i : core.getUsersList()) {
                if (i == user) {
                    i.setPassword(getPasswordHash(newPassword));
                    return true;
                }
            }
        }
        return false;
    }

    public boolean changeUserPassword(String login, String newPassword) {
        Core core = Core.getInstance();
        for (User i : core.getUsersList()) {
            if (i.getLogin().equals(login)) {
                i.setPassword(getPasswordHash(newPassword));
                return true;
            }
        }
        return false;
    }

    public boolean changeOwnPassword(String oldPassword, String newPassword) {
        if (!getPasswordHash(oldPassword).equals(admin.getPassword())) {
            return false;
        } else {
            admin.setPassword(getPasswordHash(newPassword));
            return true;
        }
        
    }
    
    public String getPasswordHash(String password) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] array = md.digest(password.getBytes());
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
						.substring(1, 3));
			}
			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
		
	}
}
